package com.baozhun.mall.common.ext

import android.app.Activity
import android.content.Context
import android.view.View
import android.view.ViewGroup
import android.view.inputmethod.InputMethodManager
import androidx.core.content.ContextCompat
import com.baozhun.mall.common.R
import com.baozhun.mall.common.model.bean.ApiPagerResponse
import com.baozhun.mall.common.util.CacheUtil
import com.chad.library.adapter.base.BaseQuickAdapter
import com.ittianyu.bottomnavigationviewex.BottomNavigationViewEx
import com.scwang.smart.refresh.layout.SmartRefreshLayout
import q.rorbin.badgeview.QBadgeView


/**
 * 作者　: hegaojian
 * 时间　: 2020/2/20
 * 描述　:项目中自定义类的拓展函数
 */


//设置适配器的列表动画
fun BaseQuickAdapter<*, *>.setAdapterAnimation(mode: Int) {
    //等于0，关闭列表动画 否则开启
    if (mode == 0) {
        this.animationEnable = false
    } else {
        this.animationEnable = true
        this.setAnimationWithDefault(BaseQuickAdapter.AnimationType.values()[mode - 1])
    }
}

fun BottomNavigationViewEx.init(navigationItemSelectedAction: (Int) -> Boolean): BottomNavigationViewEx {
    itemIconTintList = null
    enableAnimation(true)
    enableShiftingMode(false)
    enableItemShiftingMode(true)
    setTextSize(10F)
    setOnNavigationItemSelectedListener {
        navigationItemSelectedAction.invoke(it.itemId)
    }
    return this
}

var mBadgeView: QBadgeView? = null

/**
 * 添加角标
 */
fun BottomNavigationViewEx.addBadgeAt(context: Context, position: Int, number: Int): BottomNavigationViewEx {
    if (mBadgeView == null) {
        mBadgeView = QBadgeView(context)
        mBadgeView!!.setBadgeNumber(number)
            .setGravityOffset(if (CacheUtil.getAppConfig().showBrand) 18f else 35f, 2f, true)
            .setBadgeTextSize(10f, true)
            .setBadgeBackgroundColor(ContextCompat.getColor(context, com.baozhun.mall.res.R.color.color_FC0503))
            .bindTarget(this.getBottomNavigationItemView(position))
    } else {
        mBadgeView?.badgeNumber = number
    }
    return this
}

/**
 * 拦截BottomNavigation长按事件 防止长按时出现Toast ---- 追求完美的大屌群友提的bug
 * @receiver BottomNavigationViewEx
 * @param ids IntArray
 */
fun BottomNavigationViewEx.interceptLongClick(vararg ids: Int) {
    val bottomNavigationMenuView: ViewGroup = (this.getChildAt(0) as ViewGroup)
    for (index in ids.indices) {
        bottomNavigationMenuView.getChildAt(index).findViewById<View>(ids[index]).setOnLongClickListener {
            true
        }
    }
}

/**
 * 隐藏软键盘
 */
fun hideSoftKeyboard(activity: Activity?) {
    activity?.let { act ->
        val view = act.currentFocus
        view?.let {
            val inputMethodManager =
                act.getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
            inputMethodManager.hideSoftInputFromWindow(
                view.windowToken,
                InputMethodManager.HIDE_NOT_ALWAYS
            )
        }
    }
}

/**
 * 隐藏软键盘
 */
fun hideSoftKeyboard(view: View?) {
    view?.let {
        val inputMethodManager =
            it.context.getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
        inputMethodManager.hideSoftInputFromWindow(
            it.windowToken,
            InputMethodManager.HIDE_NOT_ALWAYS
        )
    }
}

/**
 * 加载列表数据
 */
fun <T> loadArrayListData(
    data: ApiPagerResponse<ArrayList<T>>?,
    baseQuickAdapter: BaseQuickAdapter<T, *>?,
    smartRefreshLayout: SmartRefreshLayout? = null
) {
    if (data?.isRefresh() == true) {
        smartRefreshLayout?.finishRefresh()
        baseQuickAdapter?.setList(data?.data)
        if (data?.last_page == 1) { //只有一页
            baseQuickAdapter?.loadMoreModule?.loadMoreEnd()
        }
    } else {
        baseQuickAdapter?.addData(data?.data ?: arrayListOf())
        if (data?.hasMore() == true) {
            baseQuickAdapter?.loadMoreModule?.loadMoreComplete()
            if (data?.last_page == 1) { //只有一页
                baseQuickAdapter?.loadMoreModule?.loadMoreEnd()
            }
        } else {
            baseQuickAdapter?.loadMoreModule?.loadMoreEnd()
        }
    }
}


/**
 * 加载列表数据
 */
fun <T> loadListData(
    data: ApiPagerResponse<List<T>>?,
    baseQuickAdapter: BaseQuickAdapter<T, *>?,
    smartRefreshLayout: SmartRefreshLayout? = null
) {
    if (data?.isRefresh() == true) {
        smartRefreshLayout?.finishRefresh()
        baseQuickAdapter?.setList(data.data)
        if (data.last_page == 1) { //只有一页
            baseQuickAdapter?.loadMoreModule?.loadMoreEnd()
        }
    } else {
        baseQuickAdapter?.addData(data?.data ?: arrayListOf())
        if (data?.hasMore() == true) {
            baseQuickAdapter?.loadMoreModule?.loadMoreComplete()
            if (data.last_page == 1) { //只有一页
                baseQuickAdapter?.loadMoreModule?.loadMoreEnd()
            }
        } else {
            baseQuickAdapter?.loadMoreModule?.loadMoreEnd()
        }
    }
}